<template>
  <el-table :data="tableData" height="780" border stripe style="width: 100%">
    <el-table-column prop="id" label="记录编号"> </el-table-column>
    <el-table-column prop="bookid" label="书籍编号" sortable> </el-table-column>
    <el-table-column prop="bookname" label="书名"> </el-table-column>
    <el-table-column prop="userid" label="借阅账号"> </el-table-column>
    <el-table-column prop="username" label="借阅者"> </el-table-column>
    <el-table-column prop="lendtime" label="借书时间"> </el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button
          class="el-icon-remove"
          size="mini"
          type="warning"
          round
          @click="
            openconfirm(scope.row.id, scope.row.bookid, scope.row.bookname)
          "
        >
          还书</el-button
        >
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
export default {
  data () {
    return {
      tableData: [],
      userid: this.$route.query.userid,
      id: '',
      bookid: ''
    }
  },
  created () {
    this.getborrow()
  },
  methods: {
    getborrow () {
      this.$ajax({
        method: 'get',
        url: '/borrow/findbyuser',
        params: { userid: this.userid }
      })
        .then(res => {
          console.log(res.data)
          this.tableData = res.data
          console.log('加载表格数据')
        })
        .catch(function (error) {
          console.log(error)
        })
    },
    openconfirm (id, bookid, bookname) {
      this.$confirm('是否还书 ' + bookname + ' ?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '还书成功!'
          })
          this.huanshu(id, bookid)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    huanshu (id, bookid) {
      this.updateborrow(id)
      this.returnbook(bookid)
    },
    updateborrow (id) {
      this.$ajax({
        method: 'put',
        url: '/borrow/borrow',
        params: { id: id }
      })
        .then(res => {
          console.log(res.data)
          this.getborrow()
        })
        .catch(function (error) {
          console.log(error)
        })
    },
    returnbook (bookid) {
      this.$ajax({
        method: 'put',
        url: '/book/returnbook',
        params: { bookid: bookid }
      })
        .then(res => {
          console.log(res.data)
        })
        .catch(function (error) {
          console.log(error)
        })
    }
  }
}
</script>

<style></style>
